package com.liuxinlong.modules.patent.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.PatentGoods;
import com.liuxinlong.modules.patent.controller.request.ImportPatentGoodsReq;
import com.liuxinlong.modules.patent.controller.request.QueryPatentGoodsReq;
import com.liuxinlong.modules.patent.controller.request.UpdatePatentGoodsReq;
import com.liuxinlong.modules.patent.service.PatentGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专利产品信息控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023-11-23
 */
@RestController
@RequestMapping("/patent-goods")
public class PatentGoodsController {

    @Autowired
    private PatentGoodsService patentGoodsService;

    @Autowired
    private UploadService uploadService;

    /**
     * 分页查询专利产品信息
     *
     * @param req 请求信息
     * @return 专利产品信息列表
     */
    @PostMapping("page")
    public BaseResp pageGoodsInfo(@RequestBody QueryPatentGoodsReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("productClass", req.getProductClass());
        queryMap.put("productModel", req.getProductModel());
        queryMap.put("region", req.getRegion());
        queryMap.put("patentSn", req.getPatentSn());
        queryMap.put("softwareName", req.getSoftwareName());
        List<Map<String, Object>> dataList = patentGoodsService.pageGoodsInfo(queryMap);
        int total = patentGoodsService.countGoods(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询专利产品信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(total);
        return resp;
    }

    /**
     * 新增专利产品信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value ="新增专利产品信息",business = LogBusinessConstants.PATENT_GOODS,operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addGoodsInfo(@RequestBody UpdatePatentGoodsReq req) {
        PatentGoods patentGoods = new PatentGoods();
        patentGoods.setProductModel(req.getProductModel());
        patentGoods.setProductClass(req.getProductClass());
        patentGoods.setRegion(req.getRegion());
        patentGoods.setRemark(req.getRemark());
        patentGoodsService.addGoodsInfo(patentGoods,req.getPatentSn(),req.getSoftwareWork());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增专利产品信息成功");
        return resp;
    }

    /**
     * 修改专利产品信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value ="修改专利产品信息",business = LogBusinessConstants.PATENT_GOODS,operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateGoodsInfo(@RequestBody UpdatePatentGoodsReq req) {
        PatentGoods patentGoods = new PatentGoods();
        patentGoods.setId(req.getId());
        patentGoods.setProductModel(req.getProductModel());
        patentGoods.setProductClass(req.getProductClass());
        patentGoods.setRegion(req.getRegion());
        patentGoods.setRemark(req.getRemark());
        patentGoodsService.updateGoodsInfo(patentGoods,req.getPatentSn(),req.getSoftwareWork());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改专利产品信息成功");
        return resp;
    }

    /**
     * 删除专利产品信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value ="删除专利产品信息",business = LogBusinessConstants.PATENT_GOODS,operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteGoodsInfo(@RequestBody BaseDeleteReq req) {
        patentGoodsService.deleteGoodsInfo(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除专利产品信息成功");
        return resp;
    }

    /**
     * 导入数据预处理
     *
     * @param multipartRequest 文件
     * @return 数据
     */
    @PostMapping("import")
    @LogAnnotation(value ="导入专利产品数据",business = LogBusinessConstants.PATENT_GOODS,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp importPatentGoods(MultipartRequest multipartRequest) {
        Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
        ImportResp resp = patentGoodsService.importGoods((String) map.get("fileRealPath"));
        return resp;
    }

    /**
     * 导入专利产品信息
     *
     * @param req 请求体
     * @return 返回导入失败数据
     */
    @PostMapping("batch-add")
    @LogAnnotation(value ="导入专利产品数据",business = LogBusinessConstants.PATENT_GOODS,operateType = LogOperateConstants.OPERATE_IMPORT)
    public ImportResp batchAddPatentGoods(@RequestBody ImportPatentGoodsReq req) {
        return patentGoodsService.batchAddGoods(req.getDataList());
    }

    /**
     * 导出专利产品信息
     *
     * @return 导出数据
     */
    @PostMapping("export")
    @LogAnnotation(value ="导出专利产品信息",business = LogBusinessConstants.PATENT_GOODS,operateType = LogOperateConstants.OPERATE_EXPORT)
    public void exportPatentGoods(@RequestBody QueryPatentGoodsReq req ,HttpServletResponse response) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("productClass", req.getProductClass());
        queryMap.put("productModel", req.getProductModel());
        queryMap.put("region", req.getRegion());
        queryMap.put("patentSn", req.getPatentSn());
        queryMap.put("softwareName", req.getSoftwareName());
        patentGoodsService.exportPatentGoods(queryMap,response);
    }
}
